// 启动一个服务
const http = require("http");
const fs = require("fs");

const server = http.createServer(function (req, res) {
  if (req.url == "/home") {
    //如果前端请求的是 /home 路径
    res.writeHead(200, {
      "content-type": "text/html;charset=utf-8",
    });
    res.end("<h1>这是首页</h1>");
  }
  if (req.url == "/user") {
    //如果前端请求的是 /user路径
    const data = fs.readFileSync("./db.json", "utf-8");
    console.log(data);
    res.end(data);
  }
});

server.listen(3001, () => {
  console.log("服务启动成功");
});
